<?php

namespace App\Jobs;

use App\Order;
use App\OrderDetail;
use App\Share;
use App\Sku;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class Double11Job implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $order;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($order)
    {
        $this->order=$order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $order = $this->order;

            $order2 = Order::with('detail')->where('id',$order->id)->where('status',1)->first();
            Log::info("order",[$order2]);
//            $order1 = [
//                'uid' => $order->uid,
//                'order_number' => $order_number,
//                'order_amount' => 0,
//                'pay_amount' => 0,
//                'coupon_id' => 0,
//                'coupon_discount' => 0,
//                'address_province' => $order2["address_province"],
//                'address_city' => $order2["address_city"],
//                'address_area' => $order2["address_area"],
//                'address_consignee' => $order2["address_consignee"],
//                'address_phone_number' =>$order2["address_phone_number"],
//                'address_detail' => $order2["address_detail"],
//                'deduction_wallet_money' => 0,
//                'deduction_wallet' => 0,
//                'quantity' => 0,
//                'is_group_buy' => 0,
//                'share_uid' => 0,
//                'group_item_id' => 0,
//                'pay_time'=>Carbon::now()->toDateTimeString(),
//                'type' => 1,
//                'status' => 1
//            ];
//            $create = Order::create($order1);

            $detail=$order2['detail'][0];
            $detail_sku=explode(",",$detail['sku']);
            $sku="#ebc9b5浅藕粉,".$detail_sku[1];
            $sku_find=Sku::where('spec_text',$sku)->where('goods_id',9)->first();
            $detail = [
                'order_id' => $order->order_number,
                'goods_name' => $detail['goods_name']."--赠品",
                'goods_id' => $detail['goods_id'],
                'cover' => $detail['cover'],
                'price' => 0,
                'number' => 1,
                'sku_id' => $sku_find['id'],
                'sku' => $sku,
                'type' => $detail['type'],
                'origin_price' => ceil($detail['origin_price']),
                'buy_type'=>1
            ];
            OrderDetail::create($detail);

            Order::where('id',$order->id)->increment('quantity',1);
            $sku_find->stock-=1;
            $sku_find->save();
        }

}
